package com.program.beautymakeup.controller;

import com.program.beautymakeup.common.AuthCheck;
import com.program.beautymakeup.domain.entity.User;
import com.program.beautymakeup.domain.form.LoginForm;
import com.program.beautymakeup.domain.vo.ResultInfo;
import com.program.beautymakeup.utils.JwtUtil;
import com.program.beautymakeup.utils.ResultInfoUtil;
import com.program.beautymakeup.utils.SecurityUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author CaiKe
 * @date 2023/2/8 20:42
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class UserController {

    private final JwtUtil jwtUtil;

    @PostMapping("/login")
    public ResultInfo<String> login(@RequestBody @Valid LoginForm loginForm){
        log.info("username【{}】, password【{}】", loginForm.getUsername(), loginForm.getPassword());
        final User user = User.builder()
                .userId(1)
                .username("root")
                .password("testtesttest")
                .role(2).build();
        return ResultInfoUtil.buildSuccessWithStr(jwtUtil.createAccessToken(user));
    }

    @GetMapping("/auth")
    @AuthCheck(0)
    public ResultInfo<Void> tryAuth(){
        log.info("{}", SecurityUtil.getCurrentUser());
        return ResultInfoUtil.buildSuccess();
    }


}